<?php
/**
 * 菜单Model类
 * @file 	MenuModel.class.php
 * @author 	itkele@gmail.com
 * @date 	2013-06-13
 * modify       
 */
class MenuModel extends Model{
	
	private $table;
	
	public function __construct(){
		parent::__construct();
		
		$this->table = C('db_table_prefix').'menu';
	}
	
	/**
	 * 获取获取全部或单个菜单
	 * @param 	int $id 
	 * @return 	array $menuList
	 */
	public function menuList($id=null){
		$menuList = array();
		$sql = "SELECT * FROM `{$this->table}` WHERE `status`=1";
		if( !empty($id) ){
			$sql .= " AND `id`='{$id}'";
		}
		$sql .= " ORDER BY `ordering` ASC,`id` ASC";
		$menuList = $this->query($sql);
		
		return $menuList;
	}
	
	//---------------------debug----------------------------
	
	/**
	 * 获取单条菜单信息
	 */
	public function getCategoryById($id){
		if(empty($id)){
			return false;
		}
		$category = $this->getCategories($id);
		
		return $category[0];
	}
		
	
	
	/**
	 * 编辑菜单
	 * @param array $data array('id'=>'','name'=>'','desc'=>'')
	 * @return boolean true or false
	 */
	public function editCategory($data){
		if(empty($data['id'])){
			return false;
		}
		$utime = time();
		$sql = "UPDATE `{$this->table}` SET `name`='{$data['name']}',`description`='{$data['description']}',`utime`='{$utime}' WHERE `id`='{$data['id']}' LIMIT 1";
		$result = $this->execute($sql);

		return $result;
	}
	
	/**
	 * 删除菜单
	 * @param int $id
	 * @return boolean true or false
	 */
	public function delCategory($id){
		if(empty($id)){
			return false;
		}
		$sql = "UPDATE `{$this->table}` SET `status`='0' WHERE `id`='{$id}' LIMIT 1";
		$result = $this->execute($sql);
		
		return $result;
	}
	
}